#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/hash_policy.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define ps putchar(' ')
#define pe putchar('\n')
#define int long long
#define eb emplace_back
#define pi pair<int, int>
#define mk make_pair
#define fi first
#define se second
#define Ad tr[p].ad
// #define Ad t[p].ad
#define ls p << 1
#define rs p << 1 | 1
// #define ls tr[p].son[0]
// #define rs tr[p].son[1]
// #define ls t[p].son[0]
// #define rs t[p].son[1]
#define len (r - l + 1)
#define mid ((l + r) >> 1)
using namespace std;
// using namespace __gnu_pbds;
inline int read()
{
    int f = 0, ans = 0;
    char c = getchar();
    while (!isdigit(c))
        f |= c == '-', c = getchar();
    while (isdigit(c))
        ans = (ans << 3) + (ans << 1) + c - 48, c = getchar();
    return f ? -ans : ans;
}
void write(int x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
const int N = 2e5 + 5, M = N << 2, inf = 1e18, mod = 998244353;

signed main()
{
    freopen("sub.in", "r", stdin);
    freopen("sub.out", "w", stdout);
    int T = read();
    while (T--)
    {
        int a = read(), b = read();
        if (!a || !b)
        {
            puts("0");
            continue;
        }
        int aa = abs(a), bb = abs(b);
        int lcm = aa / __gcd(aa, bb) * bb;
        auto solve = [&]()
        {
            int x1 = lcm / a, x2 = lcm / b;
            if (0 < x1 && x1 <= x2 || 0 < -x1 && -x1 <= -x2)
                return 0ll;
            return inf;
        };
        int ans = min({abs(a + b), aa, bb, solve()});
        swap(a, b), ans = min(ans, solve());
        write(ans), pe;
    }
    return 0;
}
